package com.ewanghuiju.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ewanghuiju.app.R;

public class EmptyView extends LinearLayout {
    View ic_error, ic_nodata;
    TextView tv_no_data;
    Button btn_error;
    ImageView iv_no_data;
    OnRefreshLisener onRefreshLisener;

    public void setOnRefreshLisener(OnRefreshLisener onRefreshLisener) {
        this.onRefreshLisener = onRefreshLisener;
    }

    public EmptyView(Context context) {
        super(context);
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.empty_view, this, true);
        initView();
        hide();
    }

    public void hide() {
        if (ic_error.getVisibility() == VISIBLE) {
            ic_error.setVisibility(GONE);
        }

        if (ic_nodata.getVisibility() == VISIBLE) {
            ic_nodata.setVisibility(GONE);
        }

    }

    private void initView() {
        iv_no_data = findViewById(R.id.iv_no_data);
        ic_error = findViewById(R.id.ic_error);
        ic_nodata = findViewById(R.id.ic_nodata);
        tv_no_data = findViewById(R.id.tv_no_data);
        btn_error = findViewById(R.id.btn_error);
        btn_error.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRefreshLisener != null) {
                    onRefreshLisener.onRefresh();
                }
            }
        });
    }

    public void showError() {
        hide();
        ic_error.setVisibility(VISIBLE);
    }

    public void showEmpty() {
        hide();
        ic_nodata.setVisibility(VISIBLE);
    }

    public void setText(String str) {
        tv_no_data.setText(str);
    }

    public void setText(String str, int icon) {
        tv_no_data.setText(str);
        iv_no_data.setImageResource(icon);
    }

    public interface OnRefreshLisener {
        void onRefresh();
    }
}
